﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Nova_Common;
using Nova_BLL;
using Nova_DAL;
using Nova_MOD;

public partial class System_Menu_MenuList : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    Nova_MOD.Menu Nova_MOD_Menu = new Nova_MOD.Menu();
    Nova_BLL.Menu Nova_BLL_Menu = new Nova_BLL.Menu();
    protected int iCount = 0;


    protected void Page_Load(object sender, EventArgs e)
    {
        LoginLogic.IsLogin();


        if (!IsPostBack)
        {
            gv_Bind();
        }
    }


    /// <summary>
    /// 部门列表
    /// </summary>
    protected void gv_Bind()
    {
        ds = Nova_BLL_Menu.DataListMenu();
        gvMenu.DataSource = ds;
        gvMenu.DataBind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnEdit_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnEdit = (ImageButton)sender;
        if (!string.IsNullOrEmpty(ibtnEdit.CommandArgument.Trim()))
        {
            int id = int.Parse(ibtnEdit.CommandArgument);
            Response.Redirect("MenuEdit.aspx?type=edit&id=" + id + "");
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnDel_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtnDel = (ImageButton)sender;
        Nova_MOD_Menu.id = Convert.ToInt32(ibtnDel.CommandArgument.Trim());

        if (!string.IsNullOrEmpty(ibtnDel.CommandArgument))
        {
            try
            {
                iCount = Nova_BLL_Menu.DeleteMenu(Nova_MOD_Menu.id.ToString());
                if (iCount > 0)
                {
                    ShowBox.MessageBox(this, "删除成功！");
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }
    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("MenuEdit.aspx");
    }

   /// <summary>
   /// 多个删除
   /// </summary>
   /// <param name="sender"></param>
   /// <param name="e"></param>
    protected void ibtnAllDel_Click(object sender, ImageClickEventArgs e)
    {

        StringBuilder sbID = new StringBuilder();
        for (int i = 0; i < this.gvMenu.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gvMenu.Rows[i].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                sbID.Append("'" + gvMenu.DataKeys[i].Value.ToString() + "',");
            }
        }
        if (sbID.Length > 0)
        {
            try
            {
                iCount = Nova_BLL_Menu.DeleteAllMenu(sbID.ToString().TrimEnd(','));
                if (iCount > 0)
                {
                    gv_Bind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }
    }

    protected void gvMenu_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {

        }
    }

    protected void gvMenu_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvMenu.PageIndex = e.NewPageIndex;
        gv_Bind();  //重新绑定GridView

    }
}
